<?php
	// Classe définissant un accès singleton aux ressources statiques HTML
	// Auteur : Sebastian Mosser


class HTMLMaker implements iSingleton {

	private static $_myHTMLMaker;
	
	private $_header;
	private $_footer;
	private $_templateTags;
	
	private $_FILEPATH    = BALLOON_FS_PATH ;// "/home/mosser/www/";
	private $_URLPATH     = BALLOON_BASE_URL; // "/~mosser/";
	private $_servicesUrl ;   //"/~mosser/services/";
	
	private $_HEADER_FILE;
	private $_FOOTER_FILE;
	
	private $_favIcon   ;
	private $_cssStyle  ;
	private $_cssLayout ;
	private $_cssDesign ;
	private $_cssPrint  ;
	private $_mainUrl   ;
	private $_phpButton ;
	private $_cssButton ;
	private $_dkwButton ;
	private $_xhtButton ;
	private $_balButton ;
	private $_gplButton ;
	private $_jsLibs    ;
	private $_mainPage   = ".:: Balloon :: 1.0 ::.";
	private $_external   = "http://ogcna.info";
	private $_webmailUrl = "http://imp.ogcnice-association.fr";
	private $_loginUrl  ;
	private $_licenceUrl;
	private $_checkFirefox = "checkFirefox();";
	
	public function getServicesUrl() { return $this->_servicesUrl; }
	
	public static function getServicesBaseUrl() {
		$tmp = self::getSingleton();
		return $tmp->getServicesUrl();
	}
	
	private function initComplexVariables() {
		$this->_HEADER_FILE = $this->_FILEPATH . "files/html/header.html";
		$this->_FOOTER_FILE = $this->_FILEPATH . "files/html/footer.html";
		
		$this->_cssStyle    = $this->_URLPATH  . "files/css/style.css";
		$this->_cssLayout   = $this->_URLPATH  . "files/css/layout.css";
		$this->_cssDesign   = $this->_URLPATH  . "files/css/design.css";
		$this->_cssPrint    = $this->_URLPATH  . "files/css/print.css";
		$this->_favIcon     = $this->_URLPATH  . "files/favicon.ico";
		$this->_jsLibs      = $this->_URLPATH  . "files/javascript";
		$this->_mainUrl     = $this->_URLPATH  . "index.php";
		$this->_phpButton   = $this->_URLPATH  . "files/logos/button-php.gif";
		$this->_cssButton   = $this->_URLPATH  . "files/logos/button-css.png";
		$this->_dkwButton   = $this->_URLPATH  . "files/logos/button-dw.png";
		$this->_xhtButton   = $this->_URLPATH  . "files/logos/button-xhtml.png";
		$this->_balButton   = $this->_URLPATH  . "files/logos/button-balloon0.9.png";
		$this->_gplButton   = $this->_URLPATH  . "files/logos/button-gpl.png";
		$this->_servicesUrl = BALLOON_BASE_URL . "services/";
		$this->_allServices = $this->_servicesUrl . "serviceDiscover/web_fDiscover.php";
		$this->_loginUrl    = $this->_servicesUrl . "login/web_fLogin.php";
		$this->_licenceUrl  = $this->_servicesUrl . "licence/web_fGPL.php" ;
	}
	
	
	private function __construct() {
		// Pour fourber l'interpreteur statique ... saloperie de PHP :@
		$this->initComplexVariables();
		
		$head = new File($this->_HEADER_FILE);
		$this->_header = $head->readAll();
		$foot = new File($this->_FOOTER_FILE);
		$this->_footer = $foot->readAll();
		
		$this->initTags();
		$this->preProcess();
	}

	public static function getSingleton() {
		if (! isset(self::$_myHTMLMaker))
			self::$_myHTMLMaker = new HTMLMaker();
		return self::$_myHTMLMaker;
	}
	
	private function initTags() {
		$this->_templateTags = array();
		$this->_templateTags["$@CSSSTYLE@$"]       = $this->_cssStyle ;
		$this->_templateTags["$@CSSLAYOUT@$"]      = $this->_cssLayout ;
		$this->_templateTags["$@CSSDESIGN@$"]      = $this->_cssDesign ;
		$this->_templateTags["$@CSSPRINT@$"]       = $this->_cssPrint;
		$this->_templateTags["$@MAINURL@$"]        = $this->_mainUrl;
		$this->_templateTags["$@MAINPAGE@$"]       = $this->_mainPage;
		$this->_templateTags["$@FAVICON@$"]        = $this->_favIcon;
		$this->_templateTags["$@EXTERNALURL@$"]    = $this->_external;
		$this->_templateTags["$@PHPBUTTON@$"]      = $this->_phpButton;
		$this->_templateTags["$@CSSBUTTON@$"]      = $this->_cssButton;
		$this->_templateTags["$@XHTMLBUTTON@$"]    = $this->_xhtButton;
		$this->_templateTags["$@DOKUWIKIBUTTON@$"] = $this->_dkwButton;
		$this->_templateTags["$@BALLOONBUTTON@$"]  = $this->_balButton;
		$this->_templateTags["$@GPLBUTTON@$"]      = $this->_gplButton;
		$this->_templateTags["$@LOGINURL@$"]       = $this->_loginUrl;
		$this->_templateTags["$@WEBMAILURL@$"]     = $this->_webmailUrl;
		$this->_templateTags["$@ALLSERVICESURL@$"] = $this->_allServices;
		$this->_templateTags["$@GPLSERVICE@$"]     = $this->_licenceUrl;
		$this->_templateTags["$@JSLIBS@$"]         = $this->_jsLibs;
	}
	
	private function preProcess() {
		foreach ($this->_templateTags as $key => $value) {
			$this->_header = str_replace($key,$value,$this->_header);
			$this->_footer = str_replace($key,$value,$this->_footer);
		}
	}
	
	private function process($dynamicTags,$string) {
		if (! is_array($dynamicTags))
			return $string;
			
	   if (empty($dynamicTags["$@MODIFIER@$"]))
		   $dynamicTags["$@MODIFIER@$"] = 
		      "<strong> .:: Balloon Powered :: <em>" . date("d/m/y :: H:i:s") . "</em> ::. </strong>";
		      
		if (empty($dynamicTags["$@JSCALLBACK@$"]))
			$dynamicTags["$@JSCALLBACK@$"] = "<!-- No user call back function defined ...-->\n";
			
		if (empty($dynamicTags["$@ONLOAD@$"]))
			$dynamicTags["$@ONLOAD@$"] = $this->_checkFirefox;
			
		foreach ($dynamicTags as $key => $value) {
			$string = str_replace($key,$value,$string);
		}
		return $string;
	}
	
	// Pour ne pas afficher la toc, on passe $toc a false
	public function makePage($content,$processedValue, $toc = True) {
		
		$page  = $this->process($processedValue,$this->_header);
		if ($toc)
			$page .= $this->addToC(); 
		$page .= $content;
		$page .= $this->process($processedValue,$this->_footer);
		return $page;
	}
	
	private function addToC() {
	   $userServices = Login::getMyServices();
	   if ($userServices == Login::$NO_SERVICES)
	      return "";
	   $res  = "\n";
	   $res .= "<!-- ToC Begin here-->\n";
	   $res .= "<div class=\"toc\">\n";
      $res .= "  <div class=\"tocheader\">\n";
      $res .= "      <script type=\"text/javascript\">showTocToggle(\"+\",\"-\")</script>\n";
      $res .= "      &nbsp;&nbsp;&nbsp;&nbsp; <a href=\"#\" onclick=\"toggleToc()\"> ";
      $res .= " .:: Mes Services ::. </a>\n";
      $res .= "   </div>\n";
      $res .= "   <div id=\"tocinside\">\n";
      $res .= "      <ul class=\"toc\">\n";
      $res .= $userServices;
      $res .= "      </ul>\n";
      $res .= "   </div>\n";
      $res .= "</div>\n";
      $res .= "<!-- ToC End here -->\n\n"; 
      return $res;
	}

}

?>